package com.hk.yajin.function.videos;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;

import com.hk.yajin.R;
import com.hk.yajin.base.BaseFragment;
import com.hk.yajin.function.videos.fragment.VideoListFragment;
import com.hk.yajin.function.videos.fragment.ViewPorintListFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：wl on 2017/6/27 15:08
 * 邮箱：wangl@ixinyongjia.com
 */
public class MenuVideoFragment extends BaseFragment {

    private MyViewPagerAdapter pagerAdapter;
    private CheckedTextView tv_video;
    private View line_left;
    private CheckedTextView tv_viewporint;
    private View line_right;
    private ViewPager vp_video;

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        mContentView = inflater.inflate(R.layout.fragment_menu_video, container, false);
        tv_video = (CheckedTextView) mContentView.findViewById(R.id.tv_video);
        tv_viewporint = (CheckedTextView) mContentView.findViewById(R.id.tv_viewporint);
        line_left = (View) mContentView.findViewById(R.id.line_left);
        line_right = (View) mContentView.findViewById(R.id.line_right);
        vp_video = (ViewPager) mContentView.findViewById(R.id.vp_video);
        tv_video.setOnClickListener(this);
        tv_viewporint.setOnClickListener(this);
        line_left.setOnClickListener(this);
        line_right.setOnClickListener(this);

        pagerAdapter = new MyViewPagerAdapter(getActivity().getSupportFragmentManager(), getFragments());
        vp_video.setAdapter(pagerAdapter);
        vp_video.setOnPageChangeListener(new MyPageChangeListener());
        vp_video.setOffscreenPageLimit(2);
        vp_video.setCurrentItem(0);
        return mContentView;
    }


    private List<Fragment> getFragments() {
        List<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(new VideoListFragment());
        fragments.add(new ViewPorintListFragment());

        return fragments;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_video:
            case R.id.line_left:
                setCheckItem(0);
                break;
            case R.id.tv_viewporint:
            case R.id.line_right:
                setCheckItem(1);
                break;
        }
    }

    private void setCheckItem(int num) {
        if (num == 0) {
            tv_video.setChecked(true);
            tv_viewporint.setChecked(false);
            line_left.setVisibility(View.VISIBLE);
            line_right.setVisibility(View.INVISIBLE);
            vp_video.setCurrentItem(0);

        } else {
            tv_viewporint.setChecked(true);
            tv_video.setChecked(false);
            line_right.setVisibility(View.VISIBLE);
            line_left.setVisibility(View.INVISIBLE);
            vp_video.setCurrentItem(1);
        }

    }


//    @OnClick({R.id.tv_video, R.id.line_left, R.id.tv_viewporint, R.id.line_right})
//    public void onViewClicked(View mContentView) {
//        switch (mContentView.getId()) {
//            case R.id.tv_video:
//            case R.id.line_left:
//                setCheckItem(0);
//                break;
//            case R.id.tv_viewporint:
//            case R.id.line_right:
//                setCheckItem(1);
//                break;
//        }
//    }

    class MyViewPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;

        /**
         * @param fm
         */
        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);

        }

        public MyViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int arg0) {
            return mFragments.get(arg0);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

    }

    class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    setCheckItem(0);
                    break;
                case 1:
                    setCheckItem(1);
                    break;

            }
        }

    }
}
